//
// Created by Zhangwj on 2023/9/26.
//

#include "FuncFParamNode.h"

std::unique_ptr<FuncFParamNode> FuncFParamNode::FuncFParamParser(Parser &parser) {
    //FuncFParam → BType Ident ['[' ']' { '[' ConstExp ']' }]
    std::unique_ptr<FuncFParamNode> funcFParamNode = std::make_unique<FuncFParamNode>();
    funcFParamNode->BType = BTypeNode::BTypeParser(parser);
    funcFParamNode->Ident = IdentNode::IdentParser(parser);
    if (parser.getLexType(0) == LexType::LBRACK) {
        // ['[' ']' { '[' ConstExp ']' }] 存在
        parser.next();
        if (parser.getLexType(0) == LexType::RBRACK) {
            parser.next();
        }
        while (parser.getLexType(0) == LexType::LBRACK) {
            funcFParamNode->ConstExps.push_back(ConstExpNode::ConstExpParser(parser));
            if (parser.getLexType(0) == LexType::RBRACK) {
                parser.next();
            }
        }
        if (parser.getLexType(0) == LexType::RBRACK) {
            parser.next();
        }
    }
    return funcFParamNode;
}
